-- OK (NESPOUSTET)
-- vymaz vazeb vedoucich na konstrukcni prvky formulare, ktere byly na verzi 109.2 smazany opravou Patch-2023-10000-000658
<g>
delete from a189234u10 where (ic=40247145 and u=10) or (ic=40247142 and u=10)
</g>


-- JN (Run)
-- move records from versions in hierarchy (delete from goods and insert to information systems)
<g>
delete a from a33753u10 as a join a180u0 as b on a.ic = b.ic and a.u = b.u;
delete a from a3460u10 as a join a180u0 as b on a.ic = b.ic and a.u = b.u;
<p>
insert into a5944000u10 (ic,u,langstate,useric,useru,ts0)
  select a.ic,a.u,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a5944000u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
insert into a12044132u10 (ic,u,langstate,useric,useru,ts0)
  select a.ic,a.u,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a12044132u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
insert into a38328578u10 (ic,u,langstate,useric,useru,ts0)
  select a.ic,a.u,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a38328578u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
</p>
<a>
insert into a5944000u10 (ic,u,sstate,langstate,useric,useru,ts0)
  select a.ic,a.u,a.sstate,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a5944000u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
insert into a12044132u10 (ic,u,sstate,langstate,useric,useru,ts0)
  select a.ic,a.u,a.sstate,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a12044132u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
insert into a38328578u10 (ic,u,sstate,langstate,useric,useru,ts0)
  select a.ic,a.u,a.sstate,a.langstate,a.useric,a.useru,a.ts0 from a180u0 as a left outer join a38328578u10 as b on a.ic = b.ic and a.u = b.u where b.ic is null and b.u is null;
</a>
</g>


--LuP: naplneni celkoveho odstaveneho mnozstvi ve stavech zasob (NESPOUSTET)
<g>
update a38243u10 set a40250773u10 = a8160728u10 where a8160728u10 is not null
update a234238u10 set a40253050u10 = a8160737u10 where a8160737u10 is not null
</g>

--ViKu : naplneni novych atributu "Identifikace uctu MD / DAL " v ucetnim deniku (NESPOUSTET)
<g>
update a set a228380u10 = a19634u10, a40046746u10 = c.ic, a40046748u10 = c.u from a4256u10 as a join a30340u10 as b on a.ic = b.ic_a and a.u = b.u_a join a4244u10 as c on b.ic_b = c.ic and b.u_b = c.u
update a set a228382u10 = a19634u10, a40046750u10 = c.ic, a40046752u10 = c.u from a4256u10 as a join a30341u10 as b on a.ic = b.ic_a and a.u = b.u_a join a4244u10 as c on b.ic_b = c.ic and b.u_b = c.u
</g>

--PeM: přesun vazeb vytvořeno z položky dokladu do zajištění (NESPOUSTET)
<g>
select a.ic,a.u,a.ic_a,a.u_a,a.ic_b,a.u_b into #temp from a1168975u10 as a 
  join a33369u10 as VOPol on (a.ic_b = VOPol.ic) and (a.u_b = VOPol.u)
  join a80961u10 as VOPolDokl on (VOPol.ic = VOPolDokl.ic_b) and (VOPol.u = VOPolDokl.u_b)
  join a4132u10 as VODokl on (VOPolDokl.ic_a = VODokl.ic) and (VOPolDokl.u_a = VODokl.u)
  join a14954u10 as VOTypDokl on (VODokl.ic = VOTypDokl.ic_a) and (VODokl.u = VOTypDokl.u_a)
  join a14952u10 as VOTyp on (VOTypDokl.ic_b = VOTyp.ic) and (VOTypDokl.u_b = VOTyp.u)

  join a30350u10 as POPol on (a.ic_a = POPol.ic) and (a.u_a = POPol.u)
  join a80961u10 as POPolDokl on (POPol.ic = POPolDokl.ic_b) and (POPol.u = POPolDokl.u_b)
  join a4128u10 as PODokl on (POPolDokl.ic_a = PODokl.ic) and (POPolDokl.u_a = PODokl.u)
  join a14954u10 as POTypDokl on (PODokl.ic = POTypDokl.ic_a) and (PODokl.u = POTypDokl.u_a)
  join a14952u10 as POTyp on (POTypDokl.ic_b = POTyp.ic) and (POTypDokl.u_b = POTyp.u)

  left join a27252u10212 as VOD on (VODokl.ic = VOD.ic) and (VODokl.u = VOD.u)
  left join a9381u10 as VOK on (VODokl.ic = VOK.ic) and (VODokl.u = VOK.u)
  left join a9424u10 as VOE on (VODokl.ic = VOE.ic) and (VODokl.u = VOE.u)
  left join a1215003u10 as VOR on (VODokl.ic = VOR.ic) and (VODokl.u = VOR.u)
  
  left join a8781u10212 as POD on (PODokl.ic = POD.ic) and (PODokl.u = POD.u)
  left join a110209u10 as POF on (PODokl.ic = POF.ic) and (PODokl.u = POF.u)
  left join a977678u10 as POR on (PODokl.ic = POR.ic) and (PODokl.u = POR.u)

where (VOTyp.a1400817u10 = 1) and 
      ((VOTyp.a8073446u10 is null) or (VOTyp.a8073446u10 in (0,1))) and
      (POTyp.a1400817u10 = 1) and
	  (VOD.ic is null) and
	  (VOK.ic is null) and
	  (VOE.ic is null) and
	  (VOR.ic is null) and
	  (POD.ic is null) and
	  (POF.ic is null) and
	  (POR.ic is null)
<a>
 and (a.sstate = 0)
</a>
</g>

<g>
 <p>
 insert into a12859570u10 (ic, u, ts0, useric, useru, langstate, ic_a, u_a, ic_b, u_b) 
 select t.ic, t.u, getdate(), 1105, 0, 1, t.ic_b, t.u_b, t.ic_a, t.u_a 
 </p>
 <a>
 insert into a12859570u10 (ic, u, ts0, useric, useru, langstate, ic_a, u_a, ic_b, u_b, sstate, sstate_a, sstate_b) 
 select t.ic, t.u, getdate(), 1105, 0, 1, t.ic_b, t.u_b, t.ic_a, t.u_a, 0, 0, 0
 </a>
 from #temp as t
</g>

<g>
 <p>
 insert into a40243931u10 (ic, u, ts0, useric, useru, langstate, ic_a, u_a, ic_b, u_b) 
 select t.ic, t.u, getdate(), 1105, 0, 1, t.ic_b, t.u_b, t.ic_a, t.u_a 
 </p>
 <a>
 insert into a40243931u10 (ic, u, ts0, useric, useru, langstate, ic_a, u_a, ic_b, u_b, sstate, sstate_a, sstate_b) 
 select t.ic, t.u, getdate(), 1105, 0, 1, t.ic_b, t.u_b, t.ic_a, t.u_a, 0, 0, 0
 </a>
 from #temp as t
</g>

<g>
  delete a from a1168975u10 as a 
    join #temp as b on (a.ic = b.ic) and (a.u = b.u)
</g>

<g>
 delete a from a1189132u9 as a
   join #temp as b on (a.ic = b.ic) and (a.u = b.u)
</g>

<g>
  drop table #temp
</g>


--PeM: přesun hodnot Zajištěno do Zajištuji (NESPOUSTET) 
<g>
update MNOZ set a40246565u10 = a209228u9 from a29558u10 as MNOZ
join a33369u10 as POLVO on (MNOZ.ic_a = POLVO.ic) and (MNOZ.u_a = POLVO.u)
join a12859570u10 as ZAJ on (POLVO.ic = ZAJ.ic_b) and (POLVO.u = ZAJ.u_b)
where (a40246565u10 is null) and (a209228u9 is not null)
</g>

<g>
update MNOZ set a209228u9 = null  from a29558u10 as MNOZ
join a33369u10 as POLVO on (MNOZ.ic_a = POLVO.ic) and (MNOZ.u_a = POLVO.u)
join a12859570u10 as ZAJ on (POLVO.ic = ZAJ.ic_b) and (POLVO.u = ZAJ.u_b)
where a209228u9 is not null
</g>

-- viz. metodický DS PeM 110.1 rozlišení zajišťované a zajišťující strany  II, (40254517,10)
-- přesypání hodnoty Zajištěno objednáním do nového atributu u všech položek objednávek vydaných, pokud vážou k zajišťovací vazbě (12859586,10) ze strany B
<g>
update MNOZ set a40246565u10 = a209228u9  from a29558u10 as MNOZ
join a33369u10 as POLVO on (MNOZ.ic_a = POLVO.ic) and (MNOZ.u_a = POLVO.u)
join a12859586u10 as ZAJ on (POLVO.ic = ZAJ.ic_b) and (POLVO.u = ZAJ.u_b)
where (a40246565u10 is null) and (a209228u9 is not null)
</g>

<g>
update MNOZ set a209228u9 = null  from a29558u10 as MNOZ
join a33369u10 as POLVO on (MNOZ.ic_a = POLVO.ic) and (MNOZ.u_a = POLVO.u)
join a12859586u10 as ZAJ on (POLVO.ic = ZAJ.ic_b) and (POLVO.u = ZAJ.u_b)
where a209228u9 is not null
</g>

